package com.ruoyi.basic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.basic.domain.PsiSupplier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basic.domain.PsiCustomer;
import com.ruoyi.basic.service.IPsiCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户Controller
 * 
 * @author smes
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/basic/customer")
public class PsiCustomerController extends BaseController
{
    @Autowired
    private IPsiCustomerService psiCustomerService;

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('basic:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(PsiCustomer psiCustomer)
    {
        startPage();
        List<PsiCustomer> list = psiCustomerService.selectPsiCustomerList(psiCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('basic:customer:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PsiCustomer psiCustomer)
    {
        List<PsiCustomer> list = psiCustomerService.selectPsiCustomerList(psiCustomer);
        ExcelUtil<PsiCustomer> util = new ExcelUtil<PsiCustomer>(PsiCustomer.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(psiCustomerService.selectPsiCustomerById(id));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('basic:customer:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PsiCustomer psiCustomer)
    {
        if (psiCustomerService.isDuplicate(psiCustomer)== false){
            return toAjax(psiCustomerService.insertPsiCustomer(psiCustomer));
        }else{
            return AjaxResult.error("名称和代码重复。");
        }
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('basic:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PsiCustomer psiCustomer)
    {
        if (psiCustomerService.isDuplicate(psiCustomer)== false){
            return toAjax(psiCustomerService.updatePsiCustomer(psiCustomer));
        }else{
            return AjaxResult.error("名称和代码重复。");
        }
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('basic:customer:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(psiCustomerService.deletePsiCustomerByIds(ids));
    }

    /**
     * 查询供应商列表
     * 不分页
     */
    @GetMapping("/dropdownList")
    public AjaxResult dropdownList()
    {
        PsiCustomer entity = new PsiCustomer();
        entity.setDelFlag("0");
        List<PsiCustomer> list = psiCustomerService.selectPsiCustomerList(entity);
        return AjaxResult.success(list);
    }
}
